#!/bin/bash

PLATFORM=my-linux-risc-qt
SCRIPT_PATH=$(pwd)

#修改源码包解压后的名称
# MAJOR_NAME=qt-everywhere-src

#修改需要下载的源码前缀和后缀
OPENSRC_VER_PREFIX=5.15
OPENSRC_VER_SUFFIX=.16
 

# #添加alsa交叉编译的动态库文件和头文件路径
# ALSA_LIB=/opt/alsa-lib-1.2.2/lib
# ALSA_INC=/opt/alsa-lib-1.2.2/include

#修改源码包解压后的名称
# PACKAGE_NAME=${MAJOR_NAME}-${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}
# PACKAGE_NAME=qtbase-5.15.16-lts-lgpl
PACKAGE_NAME=qtbase-everywhere-src-5.15.16
#定义编译后安装--生成的文件,文件夹位置路径
INSTALL_PATH=/opt/Qt5

#添加交叉编译工具链路径 
CROSS_CHAIN_PREFIX=/opt/riscv64-glibc-gcc-thead_20200702/bin/riscv64-unknown-linux-gnu

#定义压缩包名称
# COMPRESS_PACKAGE=${PACKAGE_NAME}.tar.xz
# COMPRESS_PACKAGE=v5.15.16-lts-lgpl.tar.gz
COMPRESS_PACKAGE=qtbase-everywhere-opensource-src-5.15.16.tar.xz
#无需修改--自动组合下载地址
OPENSRC_VER=${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}

# case ${OPENSRC_VER_PREFIX} in
#       5.9 | 5.12 | 5.13 | 5.14 |5.15 )
#       DOWNLOAD_LINK=http://download.qt.io/official_releases/qt/${OPENSRC_VER_PREFIX}/${OPENSRC_VER}/single/${COMPRESS_PACKAGE}
#       ;;
#    *)
#       DOWNLOAD_LINK=http://download.qt.io/new_archive/qt/${OPENSRC_VER_PREFIX}/${OPENSRC_VER}/single/${COMPRESS_PACKAGE}
#       ;;
# esac
# DOWNLOAD_LINK=https://github.com/qt/qtbase/archive/refs/tags/v5.15.16-lts-lgpl.tar.gz
DOWNLOAD_LINK=https://mirrors.ustc.edu.cn/qtproject/archive/qt/5.15/5.15.16/submodules/qtbase-everywhere-opensource-src-5.15.16.tar.xz
#无需修改--自动组合平台路径
CONFIG_PATH=${SCRIPT_PATH}/${PACKAGE_NAME}/mkspecs/${PLATFORM}

#无需修改--自动组合配置平台路径文件
CONFIG_FILE=${CONFIG_PATH}/qmake.conf

#下载源码包
do_download_src () {
  echo -e "\033[1;33mstart download ${COMPRESS_PACKAGE}...\033[0m"

  if [ ! -f "${COMPRESS_PACKAGE}" ];then
     if [ ! -d "${PACKAGE_NAME}" ];then
        wget -c ${DOWNLOAD_LINK}
     fi
  fi

  echo -e "\033[1;33mdone...\033[0m"
}

#解压源码包
do_tar_package () {
  echo -e "\033[1;33mstart unpacking the ${COMPRESS_PACKAGE} package ...\033[0m"
  if [ ! -d "${PACKAGE_NAME}" ];then
     tar -xf ${COMPRESS_PACKAGE}
  fi
  echo -e "\033[1;33mdone...\033[0m"
  cd ${PACKAGE_NAME} 
}

#安装依赖项
do_install_config_dependent () { 
  sudo chmod +777 /opt
  sudo sudo apt install libxcb-cursor0 libxcb-cursor-dev -y
  sudo apt install gcc g++ make ninja-build cmake python3 -y 
}

#修改配置平台
do_config_before () {
  echo -e "\033[1;33mstart configure platform...\033[0m"

if [ ! -d "${CONFIG_PATH}" ];then
  cp -a ${SCRIPT_PATH}/${PACKAGE_NAME}/mkspecs/linux-arm-gnueabi-g++ ${CONFIG_PATH}
fi

  echo "#" > ${CONFIG_FILE}
  echo "# qmake configuration for building with arm-linux-gnueabi-g++" >> ${CONFIG_FILE}
  echo "#" >> ${CONFIG_FILE}
  echo "" >> ${CONFIG_FILE}
  echo "MAKEFILE_GENERATOR      = UNIX" >> ${CONFIG_FILE}
  echo "CONFIG                 += incremental" >> ${CONFIG_FILE}
  echo "QMAKE_INCREMENTAL_STYLE = sublib" >> ${CONFIG_FILE}
  echo "" >> ${CONFIG_FILE}
  echo "include(../common/linux.conf)" >> ${CONFIG_FILE}
  echo "include(../common/gcc-base-unix.conf)" >> ${CONFIG_FILE}
  echo "include(../common/g++-unix.conf)" >> ${CONFIG_FILE}
  echo "" >> ${CONFIG_FILE}
  echo "# modifications to g++.conf" >> ${CONFIG_FILE}
  echo "QMAKE_CC                = ${CROSS_CHAIN_PREFIX}-gcc" >> ${CONFIG_FILE}
  echo "QMAKE_CXX               = ${CROSS_CHAIN_PREFIX}-g++" >> ${CONFIG_FILE}
  echo "QMAKE_LINK              = ${CROSS_CHAIN_PREFIX}-g++" >> ${CONFIG_FILE}
  echo "QMAKE_LINK_SHLIB        = ${CROSS_CHAIN_PREFIX}-g++" >> ${CONFIG_FILE}
  echo "" >> ${CONFIG_FILE}
  echo "# modifications to linux.conf" >> ${CONFIG_FILE}
  echo "QMAKE_AR                = ${CROSS_CHAIN_PREFIX}-ar cqs" >> ${CONFIG_FILE}
  echo "QMAKE_OBJCOPY           = ${CROSS_CHAIN_PREFIX}-objcopy" >> ${CONFIG_FILE}
  echo "QMAKE_NM                = ${CROSS_CHAIN_PREFIX}-nm -P" >> ${CONFIG_FILE}
  echo "QMAKE_STRIP             = ${CROSS_CHAIN_PREFIX}-strip" >> ${CONFIG_FILE}
  echo "load(qt_config)" >> ${CONFIG_FILE}
  echo "" >> ${CONFIG_FILE}
  echo "QMAKE_INCDIR=${INSTALL_PATH}/include" >> ${CONFIG_FILE}
  echo "QMAKE_LIBDIR=${INSTALL_PATH}/lib" >> ${CONFIG_FILE}

  cat ${CONFIG_FILE}
  echo -e "\033[1;33mdone...\033[0m"
}
 

#配置选项
do_configure () {
  echo -e "\033[1;33mstart configure ${PACKAGE_NAME}...\033[0m"

  export CC=${CROSS_CHAIN_PREFIX}-gcc
  export CXX=${CROSS_CHAIN_PREFIX}-g++  
  ./configure \
  -prefix ${INSTALL_PATH} \
  -xplatform ${PLATFORM} \
  -release \
  -opensource \
  -confirm-license \
  -no-openssl \
  -no-opengl \
  -no-xcb \
  -no-eglfs \
  -no-compile-examples \
  -no-pkg-config

  echo -e "\033[1;33mdone...\033[0m"
} 

#编译并且安装
do_make_install () {
  echo -e "\033[1;33mstart make and install ${PACKAGE_NAME} ...\033[0m"
  make -j6 && make install
  echo -e "\033[1;33mdone...\033[0m"
}

#删除下载的文件
do_delete_file () {
  cd ${SCRIPT_PATH}
  if [ -f "${COMPRESS_PACKAGE}" ];then
     sudo rm -f ${COMPRESS_PACKAGE}
  fi
}

# ver5.15.2
do_cross_make_mqtt () {

 echo -e "\033[1;33mstart make and install Mqtt ...\033[0m"
if [ ! -d "mqtt" ];then
     mkdir mqtt
fi 
cd mqtt
 
if [ ! -f "v5.15.2.tar.gz" ];then
     wget https://github.com/qt/qtmqtt/archive/refs/tags/v5.15.2.tar.gz
fi 

if [ ! -d "qtmqtt-5.15.2" ];then
   tar -xf v5.15.2.tar.gz
fi 

cd qtmqtt-5.15.2
mkdir ${INSTALL_PATH}/include/QtMqtt
cp ./src/mqtt/*.h ${INSTALL_PATH}/include/QtMqtt
/opt/Qt5/bin/qmake -spec ${PLATFORM}
make -j4
make install 

 echo -e "\033[1;33m Mqtt done...\033[0m"
}

do_download_src
do_tar_package
do_install_config_dependent
do_config_before
do_configure
do_make_install
do_cross_make_mqtt
# do_delete_file

exit $?